<?
include "include/config.php";
$location='oferte'; 
if(!isset($_SESSION['_user']))
{ 
header("location: login.php");
}


////cautare locatie
$loc=$_REQUEST['loc'];
$tip=array ('1'=>'Garsoniera','2'=>'Apartament 2 camere','3'=>'Apartament 3 camere','4'=>'Apartament 4 camere','5'=>'Teren','6'=>'Spatiu Comercial','7'=>'Casa/Vila');
$valuta=array ('0'=>'Discutabil','1'=>'RON','2'=>'EURO','3'=>'USD'); 
$oferta=array ('1'=>'Vanzare','2'=>'Inchiriere');

if($_SESSION['_level']==0) {
	$sWhere= "  ";
} else {
	$sWhere= " and user_id=".$_SESSION['_id'] . " ";
}


if ($_GET['up'])
{
if ($_POST['spof']=='on')
$spof=1;
else $spof=0;



mysql_query("update oferta set tip='".$_POST['tip']."',oferta='".$_POST['oferta']."',id_loc='".$_POST['loc']."',pret='".$_POST['pret']."',pret_tip='".$_POST['pret_tip']."',detalii='".addslashes($_POST['detalii'])."',subzona='".$_POST['nume_locatie']."',spof='".$spof."', subiect='".$_POST["subiect"]."' where id='".$_GET['up']."' ") ;

// IMAGE CROP
$sql=mysql_query("select width_t,height_t,width,height,width_m,height_m from setari");
list($width_t,$height_t,$width_n,$height_n,$width_m,$height_m)=mysql_fetch_array($sql);
$c=0;
for($i=0;$i<=50;$i++)
		{	
			if($_FILES['pictures'.$i]['name']!='')
				{	list($width, $height, $type, $attr) = getimagesize($_FILES['pictures'.$i]['tmp_name']);
					//echo $type;
					switch($type)
					{
					case 1:
						$img=imagecreatefromgif($_FILES['pictures'.$i]['tmp_name'])
						or die('xxx');
						break ;
					case 2:
						$img=imagecreatefromjpeg($_FILES['pictures'.$i]['tmp_name'])
						or die('xxx');
						break ;
					case 3:
						$img=imagecreatefrompng($_FILES['pictures'.$i]['tmp_name'])
							or die('xxx');
						break ;
					default:
						break 2;	
					} // thumbs 
				mysql_query("insert into `imagini` (id_oferta) values('".$_GET['up']."')");
				$id_poza=mysql_insert_id();
				if($width_t/$height_t<$width/$height)
				{
					$_width=$width_t;
					$_height=(int)($height*$width_t/$width);
				}
				else
				{
					$_height=$height_t;
					$_width=(int)($width*$height_t/$height);
				}
				
				$image=imagecreatetruecolor($_width,$_height);
				imagecopyresampled($image,$img,0,0,0,0,$_width+1,$_height+1,$width-1,$height-1);
				$imagename=$_GET['up'].'_'.$id_poza.".jpg";
				//imageantialias($image, true);
				imagejpeg($image,substr(srvd, 0, -6)."imagini/thumbs/".$imagename,100);
				// normal
				if($width_n/$height_n<$width/$height)
				{
					$_width=$width_n;
					$_height=(int)($height*$width_n/$width);
				}
				else
				{
					$_height=$height_n;
					$_width=(int)($width*$height_n/$height);
				}
				$image=imagecreatetruecolor($_width,$_height);
				imagecopyresampled($image,$img,0,0,0,0,$_width+1,$_height+1,$width-1,$height-1);
				//imageantialias($image, true);
				imagejpeg($image,substr(srvd, 0, -6)."imagini/normale/".$imagename,100);
				// mare
				if($width_m/$height_m<$width/$height)
					{
						$_width=$width_m;
						$_height=(int)($height*$width_m/$width);
					}
					else
					{
						$_height=$height_m;
						$_width=(int)($width*$height_m/$height);
					}
					$image=imagecreatetruecolor($_width,$_height);
					imagecopyresampled($image,$img,0,0,0,0,$_width+1,$_height+1,$width-1,$height-1);
					//imageantialias($image, true);
				imagejpeg($image,substr(srvd, 0, -6)."imagini/mari/".$imagename,100);
				mysql_query("UPDATE imagini SET src='".$imagename."' WHERE id='".$id_poza."'");
					
				}
		} 

header("location: afisare_oferta.php");
}

if ($_GET['modifica']!='')
{
$oferte_query=mysql_query("select oferta.*,locatii.name from oferta left join locatii on oferta.id_loc=locatii.id where oferta.id='".addslashes($_GET['modifica'])."'  $sWhere ");
$oferte_array=mysql_fetch_array($oferte_query);

}
//$smarty->assign('oferta_mod',$oferte_array);
if(mysql_numrows($oferte_query)==0) {
	die("nu aveti acces la aceasta oferta !");
}

if ($_GET['w']=="DEL") {
	$row=mysql_fetch_assoc(mysql_query("SELECT * FROM `imagini` WHERE id='".addslashes($_GET['idu_pic'])."'"));
	unlink(substr(srvd, 0, -6)."imagini/thumbs/".$row['src']);
	unlink(substr(srvd, 0, -6)."imagini/normale/".$row['src']);
	unlink(substr(srvd, 0, -6)."imagini/mari/".$row['src']);
	mysql_query("DELETE FROM imagini WHERE id='".$row['id']."'");
	}

$sql_main=mysql_query("select * from `imagini` WHERE id_oferta='".$_GET['modifica']."'");
	while($main=mysql_fetch_array($sql_main))
	{
		$pics[]=$main;
	}	



$loc_query=mysql_query("select id,name from locatii where id='".$loc."'");
$loc_vect=mysql_fetch_array($loc_query);
   include "include/head.php";
   


 
   
   
?>
<script language = "javascript" type = "text/javascript" src = "tiny_mce/tiny_mce.js"></script>
 <script>
        // Notice: The simple theme does not use all options some of them are limited to the advanced theme
  tinyMCE.init({
    mode : "textareas",
    theme : "advanced" ,
    theme_advanced_layout_manager : "SimpleLayout",
    mode : "exact",
    elements : "editor" ,
    theme_advanced_buttons1 : "bold,italic,underline,link,unlink,forecolor,code,justifyleft,justifyfull,justifycenter,justifyright,fontsizeselect", 
  theme_advanced_buttons2 : "bullist,numlist,separator,outdent,indent,separator,undo,redo,separator",
  theme_advanced_buttons3 : "",
  theme_advanced_toolbar_location : "top",
  theme_advanced_disable : "styleprops"  ,
   width : "400",
   force_br_newlines : "true",
 force_p_newlines : "false",
 apply_source_formatting : "true"
  }); </script>

<div id="container">
<? include "include/meniu.php"; ?> 

<div id="submenu"><div><div>
    <ul>
        <li ><a href="adauga_oferta.php">Adauga Oferta</a></li>
        <li ><a href="afisare_oferta.php">Vizualizare Oferte</a></li>
        <li id="off2"><a href="modifica_oferta.php">Modifica Oferta</a></li>
        
    </ul>
</div></div></div>
<div class="clear"></div>
<div id="content"><div id="mid">
<div>
<form enctype="multipart/form-data" action="modifica_oferta.php?up=<?=$oferte_array['id']?>" method="post" name="menu" >
<br class="clear"/>
<h2>Formular MODIFICARE Oferta</h2>
<label>Zona</label>
<div class="menu">
<ul>
    <li class="sub"><a href="#nogo"><input type="text" readonly name="nume_locatie" id="nume_locatie" value='<?=$oferte_array['subzona']?>' class="tf b"/> </a>
<?=meniu('0'); ?> 
    </li>
</ul>
</div>

<input type="hidden" name="loc" id="loc" value="<?=$oferte_array['id_loc']?>" />
    <br class="clear" />
    <label>Descriere oferta </label>
    <input type="text" name="subiect"  class="tf bbb"  value="<?=htmlentities($oferte_array['subiect'])?>" /> <br class="clear" />
    <label>Tip</label>
    <select name='tip' class='bb'>
    <? foreach ($tip as $key=>$value){
   
       if ($oferte_array['tip']==$key) $sel='selected';
       echo '<option value='.$key.' '.$sel.' >'.$value.'</option> '; $sel='';}?>
    </select> <br />
    <label>Oferta</label>
     <select name='oferta' class='bb'>
    <? foreach ($oferta as $key=>$value)  {
        
        if ($oferte_array['oferta']==$key) $sel='selected'; 
        echo '<option value='.$key.' '.$sel.'>'.$value.'</option> ';$sel='';} ?>
    </select>
    <br class="clear" />
    <label>Pret </label>
    <input type="text" name="pret"  class="tf mm" value="<?=$oferte_array['pret']?>"/>
     <select name='pret_tip' class='bb'>
    <? foreach ($valuta as $key=>$value)  {
        
        if ($oferte_array['pret_tip']==$key) $sel='selected'; 
        echo '<option value='.$key.' '.$sel.'>'.$value.'</option> ';$sel='';} ?>
    </select></span><br class="clear" />
    
    <label>Oferta speciala</label><input type="checkbox" name="spof" <? if ($oferte_array['spof']==1) echo "checked" ?> /><br />
    
    <br class="clear" />
    <label>Detalii</label>
    <textarea type="text" name="detalii"  cols="100" rows="10" class="tf " <? if ($oferte_array['spof']==1)  echo 'id=editor'?>  ><?=stripslashes($oferte_array['detalii']);?></textarea><br />
                                                      
   <label>Galerie Imagini</label>
    <div id="galerie">
        <? foreach ($pics as $pics_) {  ?>
        <div  class="pic" style="float:left"><img src="../imagini/thumbs/<?=$pics_['src']?>" alt="0" class="pict"/> 
            <br class="clear" />
            <a onclick="return delete_confirm('Sunteti sigur ca vreti sa stergeti ?')" href="modifica_oferta.php?w=DEL&idu_pic=<?=$pics_['id']?>&modifica=<?=$_GET['modifica']?>" >
        <img src="imagini/sterge.gif" alt="0" class="sterge" /> </a>
        </div>
           <?}?>
    <br />
    </div>
    <br />
    
    <br class="clear" />
    <label>Adauga imagini</label>
    <input name="pictures0" id="sel_vals_0" type="file" class="tf bb" onchange="document.getElementById('pic1').style.display='block';" />
    <? for($i=1;$i<=10;$i++) { ?>
    <div id="pic<?=$i?>" style="display:none; width:600px;">
        <label></label>
        <input name="pictures<?=$i?>" type="file" class="tf bb " onchange="document.getElementById('pic<?=($i+1)?>').style.display='block';" />
    </div>
    <? }?>
    
    
    <input type="hidden" name="update" value="1" />
<br class="clear" />
<br class="clear" />
<label>&nbsp;</label>
<input type="submit" value="ok" name="Adauga" class="ok"/>
</form>



<? if ($final!='') {?>  
<br class="clear" />
<br class="clear" />
<div class="cell__ bbbbb ac"><strong><?=$final?></strong></div>
<?}?> 





</div>
</div>
</div>
<?     include "include/footer.php";  ?> 


